<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Edit_stock
 *
 * @author admin
 */
class edit_stock extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('stock_model');
        $this->load->model('catalog');
    }

    public function index() {
        $id = $this->input->get('id');
        $this->_set_form($id);
        $this->load->view('stock/edit_stock', $this->data);
    }

    private function _set_form($id) {
        $stock = $this->stock_model->get_stock_by_id($id);
        $this->data['hid_id'] = form_hidden('id', $id);
        $this->data['txt_stock_code'] = form_input('txtStockCode', $stock[0]->stock_code, 'class="form-control"');
        $this->data['txt_stock_name'] = form_input('txtStockName', $stock[0]->stock_name, 'class="form-control"');

        $this->data['cmb_main_type'] = form_dropdown('cmbMainType', $this->catalog->list_stock_main_type(), $stock[0]->main_type_id, 'class="form-control" id="cmbMainType"');
        $this->data['cmb_sub_type'] = form_dropdown('cmbSubType', $this->catalog->list_stock_sub_type($stock[0]->main_type_id), $stock[0]->sub_type_id, 'class="form-control" id="cmbSubType"');
        $this->data['cmb_brand'] = form_dropdown('cmbBrand', $this->catalog->list_stock_brand($stock[0]->sub_type_id), $stock[0]->brand_id, 'class="form-control" id="cmbBrand"');

        $this->data['txt_stock_price'] = form_input('txtStockPrice', $stock[0]->stock_price, 'class="form-control"');
        $this->data['txt_stock_qty'] = form_input('txtStockQty', $stock[0]->stock_qty, 'class="form-control"');
        $this->data['txt_stock_classifire'] = form_input('txtClassifire', $stock[0]->classifire, 'class="form-control"');
        $this->data['txt_alert_below'] = form_input('txtAlertBelow', $stock[0]->alert_below, 'class="form-control"');
        $this->data['txt_stock_detail'] = form_textarea(array('name' => 'txtStockDetail', 'rows' => '3'), $stock[0]->stock_detail, 'class="form-control" style="resize: none;"');
        $this->data['upload_pic'] = form_upload('uploadPic', '', 'style="margin-left:30px"');
        $img = $this->stock_model->get_stock_img_by_id($id);
        if ($img != NULL) {
            $this->data['img_picture'] = '<img src="' . base_url() . 'uploads/stocks/' . $img[0]->file_name . '" style="width:355px;"  \>';
        } else {
            $this->data['img_picture'] = '<img src="' . base_url() . 'images/driver/no_image.jpg" style="width:355px"  \>';
        }
        $this->_set_error();
    }

    private function _set_error() {
        $this->data['err_stock_name'] = form_error('txtStockName', '<div class="error">', '</div>');
        $this->data['err_main_type'] = form_error('cmbMainType', '<div class="error">', '</div>');
        $this->data['err_sub_type'] = form_error('cmbSubType', '<div class="error">', '</div>');
        $this->data['err_brand'] = form_error('cmbBrand', '<div class="error">', '</div>');
        $this->data['err_stock_price'] = form_error('txtStockPrice', '<div class="error">', '</div>');
        $this->data['err_stock_qty'] = form_error('txtStockQty', '<div class="error">', '</div>');
        $this->data['err_stock_classifire'] = form_error('txtClassifire', '<div class="error">', '</div>');
        $this->data['err_alert_below'] = form_error('txtAlertBelow', '<div class="error">', '</div>');
    }

    public function validate() {

        $utility = new Config_form();

        $this->form_validation->set_rules('txtStockCode', 'Code', '');
        $this->form_validation->set_rules('txtStockName', 'Name', 'required');
        $this->form_validation->set_rules('cmbMainType', 'Main Type', 'required');
        $this->form_validation->set_rules('cmbSubType', 'Sub Type', 'required');
        $this->form_validation->set_rules('cmbBrand', 'Brand', 'required');
        $this->form_validation->set_rules('txtStockPrice', 'Price', 'required|numeric');
        $this->form_validation->set_rules('txtStockQty', 'Qty', 'required|numeric');
        $this->form_validation->set_rules('txtClassifire', 'Classifire', 'required');
        $this->form_validation->set_rules('txtStockDetail', 'Detail', '');
        $this->form_validation->set_rules('txtAlertBelow', 'Alert Below', 'required|numeric');


        if ($this->form_validation->run() == FALSE) {
            $this->_set_form();
            $this->load->view('stock/add_stock', $this->data);
        } else {
            $id = $this->input->post('id');
            $stock_code = $this->input->post('txtStockCode');
            $stock_name = $this->input->post('txtStockName');
            $main_type_id = $this->input->post('cmbMainType');
            $sub_type_id = $this->input->post('cmbSubType');
            $brand_id = $this->input->post('cmbBrand');
            $stock_price = $this->input->post('txtStockPrice');
            $stock_qty = $this->input->post('txtStockQty');
            $stock_classifire = $this->input->post('txtClassifire');
            $stock_detail = $this->input->post('txtStockDetail');
            $alert_below = $this->input->post('txtAlertBelow');

            $config['upload_path'] = './uploads/stocks/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['overwrite'] = FALSE;
            $config['encrypt_name'] = TRUE;

            $this->load->library('upload', $config);
            if ($_FILES['uploadPic']['name'] != NULL) {
                if (!$this->upload->do_upload('uploadPic')) {
                    $this->data['err_upload'] = $this->upload->display_errors('<div class="error">', '</div>');
                    $this->_set_form();
                    $this->load->view('stock/edit_stock', $this->data);
                } else {
                    //To save
                    $upload_data = $this->upload->data();
                    $is_result = $this->stock_model->update_data($id, $stock_name, $main_type_id, $brand_id, $stock_price, $stock_qty, $stock_classifire, $alert_below, $stock_code, $sub_type_id, $stock_detail);
                    $img = $this->stock_model->get_stock_img_by_id($id);
                    if ($img != NULL) {
                        $this->stock_model->update_image($id, $upload_data['file_name']);
                    } else {
                        $this->stock_model->save_image($id, $upload_data['file_name']);
                    }

                    if ($is_result == TRUE) {
                        echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'stock/home');
                        exit();
                    } else {
                        $this->_set_form();
                        $this->load->view('stock/edit_stock', $this->data);
                    }
                }
            } else {
                $is_result = $this->stock_model->update_data($id, $stock_name, $main_type_id, $brand_id, $stock_price, $stock_qty, $stock_classifire, $alert_below, $stock_code, $sub_type_id, $stock_detail);
                if ($is_result == TRUE) {
                    echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'stock/home');
                    exit();
                } else {
                    $this->_set_form();
                    $this->load->view('stock/edit_stock', $this->data);
                }
            }
        }
    }

}
